package test;

import com.alibaba.fastjson.JSON;

import java.io.*;

public class SerialTest2 implements Serializable{
    public byte b=8;
    public int i=16;
    public Contain1 c=new Contain1();

    public SerialTest2(){}
    public String toString(){
        return JSON.toJSONString(this);
    }
    public void setB(int t){
        b+=t;
    }
//    private void writeObject(ObjectOutputStream oos) throws IOException{
//        oos.writeByte(b);
//        oos.writeInt(i);
//
//
//    }
//    private void readObject(ObjectInputStream ois) throws IOException,ClassNotFoundException{
//        this.b=ois.readByte();
//        this.i=ois.readInt();
//
//    }
    public static void main(String arg[]) throws Exception{
        SerialTest2 st2=new SerialTest2();
        //st2.setB(5);
        System.out.println(st2);

        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("jjj.txt"));
        oos.writeObject(st2);

        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("jjj.txt"));
        st2=(SerialTest2)ois.readObject();
        System.out.println(st2);

        FileInputStream fis=new FileInputStream("jjj.txt");
        byte[] data=fis.readAllBytes();

        for(int i=0;i<data.length;i++){
            System.out.printf("%02x ",data[i]);
            if((i+1)%16==0)
                System.out.println();
        }
    }

//    @Override
//    public void writeExternal(ObjectOutput out) throws IOException {
//        out.writeInt(i);
//        out.writeByte(b);
//    }
//
//    @Override
//    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
//        this.i=in.readInt();
//        this.b=in.readByte();
//    }
}
class Contain1 implements Serializable{
    public int num=3;
    public Contain1(){}

    public String toString(){
        return JSON.toJSONString(this);
    }
}
